package com.woniuxy.certified.web.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniuxy.certified.dao.ApplicationForm;
import com.woniuxy.certified.model.dto.CertifiedAccountDto;
import com.woniuxy.certified.model.form.ApplicationsForm;
import com.woniuxy.certified.model.form.PassAllApplicationForm;
import com.woniuxy.certified.model.param.ApplicationParam;
import com.woniuxy.certified.model.param.PassAllApplicationParam;
import com.woniuxy.certified.service.ApplicationFormService;
import com.woniuxy.core.model.Result;
import com.woniuxy.core.util.JwtUtil;
import com.woniuxy.core.util.PageInfo;
import com.woniuxy.platform.model.form.LoadAllpaltforminfoForm;
import com.woniuxy.platform.model.param.LoadAllpaltforminfoParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 凌玉春
 * @since 2024年01月15日
 */
@RestController
@RequestMapping("/applicationForm")
@Api(tags = "物业申请入住接口")
public class ApplicationFormController {


    @Resource
    private ApplicationFormService applicationFormService;

    @GetMapping("/test")
    public Result<String> test(){
        applicationFormService.test();
        return Result.getResult(String.class).setMessage("测试成功");
    }

    @ApiOperation("申请成为物业")
    @PostMapping("/applicationForProperty")
    public Result applicationForProperty(@Validated @RequestBody ApplicationsForm af, BindingResult br){
        if (br.hasErrors()) {
            // 如果有验证错误发生，可以获取所有错误消息
            StringBuilder errorMessage = new StringBuilder();
            br.getAllErrors().forEach(error -> {
                errorMessage.append(error.getDefaultMessage()).append("!");
            });
            return Result.getResult(Boolean.class).setMessage(errorMessage.toString()).setData(false);
        }
        System.out.println(af);
        ApplicationParam param = BeanUtil.toBean(af, ApplicationParam.class);
        applicationFormService.applicationForProperty(param);
        return Result.getResult().setMessage("提交申请成功");
    }

    @ApiOperation("上传物业营业执照")
    @PostMapping("/uploadImage")
    public Result<String> uploadImage(MultipartFile image) throws IOException {
       String url = applicationFormService.uploadImage(image);
        return Result.getResult(String.class).setMessage("上传成功").setData(url);
    }


    @ApiOperation("平台通过物业入住申请")
    @GetMapping("/passApplication")
    @ApiImplicitParam(name = "id",value = "物业申请表id")
    public Result<CertifiedAccountDto> passApplication(Integer id){
        CertifiedAccountDto dto = applicationFormService.passApplication(id);
        return Result.getResult(CertifiedAccountDto.class).setMessage("成功通过").setData(dto);
    }

    @ApiOperation("平台查询所有物业入驻申请")
    @GetMapping("/passAllApplication")
    public Result<PageInfo> passAllApplication(PassAllApplicationForm laf){
        PassAllApplicationParam param = BeanUtil.toBean(laf, PassAllApplicationParam.class);
        PageInfo<ApplicationForm> pageInfo =applicationFormService.passAllApplication(param);
        return Result.getResult(PageInfo.class).setMessage("平台查询所有物业入驻申请列表成功").setData(pageInfo);
    }

    @ApiOperation("平台通过物业名字查找物业")
    @GetMapping("/LoadcertifiedBycertifiedInfoName")
    @ApiImplicitParam(name = "certifiedInfoName",value = "物业名字")
    public Result LoadcertifiedBycertifiedInfoName(String certifiedInfoName){
        ApplicationForm applicationForm= applicationFormService.LoadcertifiedBycertifiedInfoName(certifiedInfoName);
        return Result.getResult(ApplicationForm.class).setMessage("平台通过账号查找物业成功").setData(applicationForm);
    }


    @ApiOperation("平台拒绝物业入住申请")
    @GetMapping("/refuseApplication")
    @ApiImplicitParam(name = "id",value = "物业申请表id")
    public Result<String> refuseApplication(Integer id){
        applicationFormService.refuseApplication(id);
        return Result.getResult(String.class).setMessage("拒绝通过");
    }



}

